//
//  JMMuenView.m
//  ShareClien
//
//  Created by xiaorui on 14-12-22.
//  Copyright (c) 2014年 xiaorui. All rights reserved.
//

#import "JMMuenView.h"
#import "JMHeader.h"
#import "UIViewExt.h"


@interface JMMuenView ()


@property (nonatomic,strong) UIImageView *imView;
@end


@implementation JMMuenView
@synthesize menuitem = _menuitem;
@synthesize rotate = _rotate;
@synthesize titleLabel = _titleLabel;




-(id)initWithFrame:(CGRect)frame withItem:(JMMuenViewItem *)menuitem{
    self = [super initWithFrame:frame];
    if (self){
        self.menuitem =menuitem;
        UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickMyself:)];
        [self addGestureRecognizer:tap];
        self.layer.masksToBounds = YES;
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width - 23, frame.size.height)];
        self.titleLabel.backgroundColor = [UIColor clearColor];
        self.titleLabel.text = menuitem.title;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.textColor = UIColorFromRGB(0x2c2c2c);
        [self addSubview:self.titleLabel];
        
        self.imView =[[UIImageView alloc]initWithFrame:CGRectMake(self.center.x + 50, frame.size.height/2.0 - 3, 11.5, 6.5)];
        self.imView.userInteractionEnabled = YES;
        self.imView.image = menuitem.image;
        self.imView.backgroundColor =[UIColor clearColor];
        [self addSubview:self.imView];
        _rotate  = YES;
    }
    return self;
}
-(void)clickMyself:(UIGestureRecognizer *)gesture{
    
    [self.menuitem.target performSelectorOnMainThread:self.menuitem.action withObject:self waitUntilDone:YES];
    
    [self rotating];
}
-(void)rotating{    
    if(_rotate){
        [UIView animateWithDuration:.25 animations:^{
            self.imView.transform = CGAffineTransformMakeRotation((-180.0f * -M_PI) / 180.0f);
            _rotate = !_rotate;
        }];
    }else{
        [UIView animateWithDuration:.25 animations:^{
            self.imView.transform = CGAffineTransformIdentity;
            _rotate = !_rotate;
            
        }];
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end


@implementation JMMuenViewItem

-(id)initWithImage:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)action;
{
    self = [super init];
    if(self){
        self.image = image;
        self.target = target;
        self.action = action;
        self.title = title;
    }
    return self;
}


@end